package com.qianfeng.wuyang.himalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.adapter.recommendadapter.TodayHottestAdapter;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.HottestTopEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TodayHottestCategoryEntity;
import com.qianfeng.wuyang.himalaya.callback.recommendcallback.TopHottestService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 今日最火的
 * A simple {@link Fragment} subclass.
 */
public class TodayHottestFragment extends Fragment implements Callback<HottestTopEntity>, View.OnClickListener {
    private  View view;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    private List<TodayHottestCategoryEntity> categoryList = new ArrayList<>();

    private TodayHottestAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        MainActivity mainactivity= (MainActivity) getActivity();

//            view = inflater.inflate(R.layout.fragment_today_hottest,container,false);
//            viewPager= (ViewPager) mainactivity.drawerlayout.findViewById(R.id.discovery_today_viewpager);
//            tabLayout = (TabLayout) mainactivity.drawerlayout.findViewById(R.id.discovery_today_tablayout);
//            ImageView imgback= (ImageView) mainactivity.drawerlayout.findViewById(R.id.back_todayhottest);
////            imgback.setOnClickListener(this);
//        Log.d("1620","todathottestoncreatv");

        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/track?
// device=android&key=ranking:track:scoreByTime:1:0&pageId=1&pageSize=20&scale=2
        TopHottestService service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(TopHottestService.class);
        service.getTopHottest("android","ranking:track:scoreByTime:1:0",1,20,2).enqueue(this);

        adapter = new TodayHottestAdapter(getActivity().getSupportFragmentManager(),getContext(),"ranking:track:scoreByTime:1:0");
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

    }


    @Override
    public void onResponse(Call<HottestTopEntity> call, Response<HottestTopEntity> response) {
        Log.d("1620",response.body().toString());
        if (response.body()!=null) {
            categoryList.addAll(response.body().getCategories());
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<HottestTopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
//        getActivity().finish();
    }
}
